package com.swt.dao.Tdao.impl;

import com.swt.dao.Tdao.VipStaffDao;
import com.swt.domain.User;
import com.swt.domain.Vipcard;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
@Repository(value = "TVipDao")
public class VipStaffDaoImpl implements VipStaffDao {
    @Resource
    private HibernateTemplate hibernateTemplate;


    @Override
    public List<User> queryDao(int page, String query,int clubClubId) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        String sql = "SELECT * from user where user_id IN(SELECT vipcard.vipcard_userId from vipcard WHERE vipcard_bossid=? GROUP BY vipcard_userId) ";

        if (query!=null){
            sql = sql+"limit "+page+","+9+"  and user_name like '%"+query+"%' ";
        }else {
            sql = sql+"limit "+page+","+9;

        }
        System.out.println(sql);

        List<User> vipcardList = session.createSQLQuery(sql).setParameter(0,clubClubId).list();
        return vipcardList;

    }

    @Override
    public List<Vipcard> queryVipCardDao(int userid) {
        SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();

        List<Vipcard> vipcardList= session.createQuery("from Vipcard where vipcardUserId = ?").setParameter(0,userid).list();

        return vipcardList;
    }

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
}
